package com.xbongbong.pro.enums;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * api-webHook支持的业务
 * @author 李少杰
 * @version v1.0.0
 * @date 2021/9/17 16:37
 */
@Getter
public enum ApiCallBackEnum {

    /**
     * 客户
     */
    CUSTOMER(1, XbbRefTypeEnum.CUSTOMER, XbbRefTypeEnum.CUSTOMER.getAlias(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 合同
     */
    CONTRACT(2, XbbRefTypeEnum.CONTRACT, XbbRefTypeEnum.CONTRACT.getAlias(), XbbRefTypeEnum.CONTRACT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 销售机会
     */
    SALES_OPPORTUNITY(3, XbbRefTypeEnum.SALES_OPPORTUNITY, XbbRefTypeEnum.SALES_OPPORTUNITY.getAlias(), XbbRefTypeEnum.SALES_OPPORTUNITY.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 自定义表单
     */
    SYSTEM(4, XbbRefTypeEnum.SYSTEM, XbbRefTypeEnum.SYSTEM.getAlias(), XbbRefTypeEnum.PAAS.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 应收款
     */
    PAYMENT(5, XbbRefTypeEnum.PAYMENT, XbbRefTypeEnum.PAYMENT.getAlias(), XbbRefTypeEnum.PAYMENT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 回款单
     */
    PAYMENT_SHEET(6, XbbRefTypeEnum.PAYMENT_SHEET, XbbRefTypeEnum.PAYMENT_SHEET.getAlias(), XbbRefTypeEnum.PAYMENT_SHEET.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 产品
     */
    PRODUCT(7, XbbRefTypeEnum.PRODUCT, XbbRefTypeEnum.PRODUCT.getAlias(), XbbRefTypeEnum.PRODUCT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 线索
     */
    CLUE(8, XbbRefTypeEnum.CLUE, XbbRefTypeEnum.CLUE.getAlias(), XbbRefTypeEnum.CLUE.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 客户跟进记录
     */
    CUSTOMER_COMMUNICATE(9, XbbRefTypeEnum.CUSTOMER_COMMUNICATE, XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getAlias(), XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 仓库
     */
    WAREHOUSE(10, XbbRefTypeEnum.WAREHOUSE, XbbRefTypeEnum.WAREHOUSE.getAlias(), XbbRefTypeEnum.WAREHOUSE.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 联系人
     */
    CONTACT(11, XbbRefTypeEnum.CONTACT, XbbRefTypeEnum.CONTACT.getAlias(), XbbRefTypeEnum.CONTACT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 采购合同
     */
    PURCHASE(12, XbbRefTypeEnum.PURCHASE, XbbRefTypeEnum.PURCHASE.getAlias(), XbbRefTypeEnum.PURCHASE.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 产品分类
     */
    PRODUCT_CATEGORY(13, XbbRefTypeEnum.PRODUCT_CATEGORY, XbbRefTypeEnum.PRODUCT_CATEGORY.getAlias(), XbbRefTypeEnum.PRODUCT_CATEGORY.getName(), EnableEnum.CLOSE.getCode()),

    /**
     * 报价单
     */
    QUOTATION(14, XbbRefTypeEnum.QUOTATION, XbbRefTypeEnum.QUOTATION.getAlias(), XbbRefTypeEnum.QUOTATION.getName(), EnableEnum.OPEN.getCode()),


    /**
     * 竞争对手
     * 还没有做完 合并代码仍旧注释
     */
//    COMPETITOR(15,XbbRefTypeEnum.COMPETITOR,XbbRefTypeEnum.COMPETITOR.getAlias(),XbbRefTypeEnum.COMPETITOR.getName(),EnableEnum.OPEN.getCode()),

    /**
     * 服务项目
     * 还没有做完 合并代码仍旧注释
     */
//    SERVICE_PROJECT(16, XbbRefTypeEnum.SERVICE_PROJECT, XbbRefTypeEnum.SERVICE_PROJECT.getAlias(), XbbRefTypeEnum.SERVICE_PROJECT.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 供应商
     */
    SUPPLIER(17, XbbRefTypeEnum.SUPPLIER, XbbRefTypeEnum.SUPPLIER.getAlias(), XbbRefTypeEnum.SUPPLIER.getName(), EnableEnum.OPEN.getCode()),

    /**
     * 工单
     */
    WORK_ORDER(18, XbbRefTypeEnum.WORK_ORDER, XbbRefTypeEnum.WORK_ORDER.getAlias(), XbbRefTypeEnum.WORK_ORDER.getName(), EnableEnum.OPEN.getCode()),

    ;




    /**
     * 枚举唯一码
     */
    private final Integer code;

    /**
     * 业务类型
     */
    private final XbbRefTypeEnum xbbRefTypeEnum;

    /**
     * 回调
     */
    private final String alias;

    /**
     * 回调名称
     */
    private final String name;

    /**
     * 是否启用api回调
     */
    private final Integer enable;


    ApiCallBackEnum(Integer code, XbbRefTypeEnum xbbRefTypeEnum, String alias, String name, Integer enable) {
        this.code = code;
        this.xbbRefTypeEnum = xbbRefTypeEnum;
        this.alias = alias;
        this.name = name;
        this.enable = enable;
    }

    public static ApiCallBackEnum getByCode(Integer code) {
        if(code == null) {
            return null;
        }
        for (ApiCallBackEnum cacheCode : values()) {
            if (Objects.equals(cacheCode.getCode(), code)) {
                return cacheCode;
            }
        }
        return null;
    }

    public static ApiCallBackEnum getByBusiness(XbbRefTypeEnum xbbRefTypeEnum) {
        if(xbbRefTypeEnum == null) {
            return null;
        }
        for (ApiCallBackEnum cacheCode : values()) {
            if (Objects.equals(cacheCode.getXbbRefTypeEnum(), xbbRefTypeEnum)) {
                return cacheCode;
            }
        }
        return null;
    }

    public static ApiCallBackEnum getByAlias(String alias) {
        if(alias == null) {
            return null;
        }
        for (ApiCallBackEnum cacheCode : values()) {
            if (Objects.equals(cacheCode.getAlias(), alias)) {
                return cacheCode;
            }
        }
        return null;
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }

    public static List<ApiCallBackEnum> getAllEnum() {
        List<ApiCallBackEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static JSONObject getTplusCallbackModule() {
        JSONObject tplusCallbackModule = new JSONObject();
        tplusCallbackModule.put(CUSTOMER.alias, 1);
        tplusCallbackModule.put(CONTRACT.alias, 1);
        tplusCallbackModule.put(SALES_OPPORTUNITY.alias, 0);
        tplusCallbackModule.put(SYSTEM.alias, 0);
        tplusCallbackModule.put(PAYMENT.alias, 0);
        tplusCallbackModule.put(PAYMENT_SHEET.alias, 0);
        tplusCallbackModule.put(PRODUCT.alias, 0);
        tplusCallbackModule.put(CLUE.alias, 0);
        tplusCallbackModule.put(CUSTOMER_COMMUNICATE.alias, 0);
        tplusCallbackModule.put(WAREHOUSE.alias, 0);
        tplusCallbackModule.put(CONTACT.alias, 1);
        tplusCallbackModule.put(PURCHASE.alias, 0);
        return tplusCallbackModule;
    }
}
